<template>
  <div class="m-4">
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <span class="px-2"></span>
        <Tabs v-model:activeKey="state.keyActiveTab" @change="onTabChange">
          <TabPane key="1" tab="余额" />
          <TabPane key="2" tab="积分" />
        </Tabs>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleCreate">新增</a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                icon: 'clarity:note-edit-line',
                tooltip: '编辑信息',
                onClick: handleEdit.bind(null, record),
              },
              {
                icon: 'ant-design:delete-outlined',
                color: 'error',
                tooltip: '删除信息',
                popConfirm: {
                  title: '是否确认删除',
                  placement: 'left',
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <ModalAdd @register="registerModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup>
  import { reactive } from 'vue';
  import { BasicTable, TableAction, useTable } from '@/components/Table';
  import { TabPane, Tabs } from 'ant-design-vue';
  import { useModal } from '@/components/Modal';
  import { ColumnPage, SearchForm } from './data';
  import ModalAdd from './ModalAdd.vue';
  import { useRouter } from 'vue-router';
  import { getMemberPage } from '@/modules/member/api';

  const router = useRouter();
  const state = reactive({
    keyActiveTab: '1',
  });

  //defineOptions({ name: 'AccountManagement' });
  const [registerTable, { reload, updateTableDataRecord, setProps }] = useTable({
    title: '商品列表',
    api: getMemberPage,
    rowKey: 'id',
    columns: ColumnPage,
    formConfig: {
      labelWidth: 120,
      schemas: SearchForm,
      autoSubmitOnEnter: true,
    },
    useSearchForm: true,
    showTableSetting: true,
    bordered: true,
    actionColumn: {
      width: 120,
      title: '操作',
      dataIndex: 'action',
    },
  });

  //add+++++++++++++++++++++++++++++
  const [registerModal, { openModal }] = useModal();

  function handleCreate() {
    openModal(true, {
      isUpdate: false,
    });
  }

  //edit
  function handleEdit(record: any) {
    console.log(record);
    openModal(true, {
      record,
      isUpdate: true,
    });
  }

  //view
  function handlePreview(record: Recordable) {
    router.push({ path: '/member/manage/info', query: { memberId: record.memberId } });
  }

  //delete
  function handleDelete(record: Recordable) {
    console.log(record);
  }

  function handleSuccess({ isUpdate, values }) {
    if (isUpdate) {
      // 演示不刷新表格直接更新内部数据。
      // 注意：updateTableDataRecord要求表格的rowKey属性为string并且存在于每一行的record的keys中
      const result = updateTableDataRecord(values.id, values);
      console.log(result);
    } else {
      reload();
    }
  }

  //tab
  const onTabChange = () => {
    console.log(state.keyActiveTab);
    setProps({
      searchInfo: {
        tabType: state.keyActiveTab,
      },
    });
    reload();
  };
</script>
<style lang="less" scoped>
  .tab-active {
    border-bottom: 2px @primary-color solid;
    color: @primary-color;
    padding-bottom: 5px;
    font-weight: bold;
  }
</style>
